package com.huifaapp.mall.selectImage;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;



import java.io.File;
import java.util.Date;

public class SelectPicMenu implements AdapterView.OnItemClickListener{
	public static final int FLAG_CHOOSE_IMG = 10;
	public static final int FLAG_CHOOSE_PHONE = 20;
	private int selectImageNum = 1;
	Activity activity;
	ScreenMenu menu;
	private boolean isOperate;
	private String localTempImageFileName = "";
	private OnSelectMenuClickListener mOnSelectMenuClickListener;

	public SelectPicMenu(Activity activity) {
		this.activity = activity;
		menu = new ScreenMenu(activity);
		menu.setTitle("选择照片");
	}

	public String getFileImageName(){
		return RootFile.getFileObj().getCacheFiles().getPath() + "/" + localTempImageFileName;
	}

	public void setSelectImageNum(int selectImageNum) {
		this.selectImageNum = selectImageNum;
	}

	public void setOnSelectMenuClickListener(OnSelectMenuClickListener listener) {
		this.mOnSelectMenuClickListener = listener;
	}

	public interface OnSelectMenuClickListener{
		public int onClick();
	}

	/**
	 * 是否选择1张还是多张
	 * @param isOperate
	 */
	public void setIsOperate(boolean isOperate) {
		this.isOperate = isOperate;
	}

	public void create(){
		SelectAdapter adapter = new SelectAdapter(activity, new String[]{"拍照", "相册"}, true);
		menu.setAdapter(adapter);
		menu.setOnItemClickListener(this);
		menu.create();
	}

	public void show(){
		menu.show();
	}

	public void dismiss(){
		menu.dismiss();
	}

	public void takePhoto(){
		if (mOnSelectMenuClickListener != null) {
			selectImageNum = mOnSelectMenuClickListener.onClick();
			if (selectImageNum == 0) {
				dismiss();
				//activity.showMsg("选择图片数量为0");
				return;
			}
		}
		if (!RootFile.checkSdCardState()) {
			//activity.showMsg("未检测到SD卡或SD卡已损坏");
			return;
		}
		String status = Environment.getExternalStorageState();
		if (status.equals(Environment.MEDIA_MOUNTED)) {
			try {
				localTempImageFileName = "";
				localTempImageFileName = String.valueOf((new Date()).getTime()) + ".png";
				File filePath = RootFile.getFileObj().getCacheFiles();
				if (!filePath.exists()) {
					filePath.mkdirs();
				}
				//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				File f = new File(filePath, localTempImageFileName);
        /*Uri u = Uri.fromFile(f);
        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
        activity.startActivityForResult(intent, FLAG_CHOOSE_PHONE);*/


				f.getParentFile().mkdirs();
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
				Uri mImageCaptureUri;
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果是7.0android系统
					ContentValues contentValues = new ContentValues(1);
					contentValues.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
					mImageCaptureUri= activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
							contentValues);
				}else{
					mImageCaptureUri = Uri.fromFile(f);
				}
				intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
				activity.startActivityForResult(intent, FLAG_CHOOSE_PHONE);
			} catch (Exception e) {
				//activity.showMsg("获取图片出错!");
			}
		}
	}

	public void selectPhoto(){
		if (mOnSelectMenuClickListener != null) {
			selectImageNum = mOnSelectMenuClickListener.onClick();
			if (selectImageNum == 0) {
				dismiss();
				//activity.showMsg("选择图片数量为0");
				return;
			}
		}
		if (!RootFile.checkSdCardState()) {
			//activity.showMsg("未检测到SD卡或SD卡已损坏");
			return;
		}
		//从相册获取图片
		Intent intent = new Intent(activity, ChooseImageActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt(ChooseImageActivity.INTENT_PARAMETER_IMAGECOUNT, selectImageNum);
		bundle.putBoolean(ChooseImageActivity.INTENT_PARAMETER_ISOPERATE, isOperate);
		bundle.putString(ChooseImageActivity.INTENT_PARAMETER_RETURNCLASS, activity.getClass().getName());
		intent.putExtras(bundle);
		activity.startActivityForResult(intent, FLAG_CHOOSE_IMG);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		if (position == 0) {
			takePhoto();
		} else if (position == 1) {
			selectPhoto();
      /*PhotoPickerIntent intent = new PhotoPickerIntent(activity);
      intent.setPhotoCount(9);
      intent.setShowCamera(false);
      activity.startActivityForResult(intent, 0x145);*/
		}
	}
}
